// pages/sign/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: " ",
    bindpover: true,//是否手机号输入完成
    bindvover: true,//是否验证码输入完成
    phonenumber_regtrue: false,//电话号码是否符合正则要求
    code_regtrue: false,//验证码是否符合正则要求
    uid: "",//对应内部id
    sign: false,//是否注册过
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.getInternalUserInfo_request();
    if (that.data.sign) {
      wx.setNavigationBarTitle({
        title: "登录",

      })
  
     

    } else {
      wx.setNavigationBarTitle({
        title: "登录/注册"
      })
   
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 


  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
 

  getInternalUserInfo_request: function () {
    var that = this;

    var yuyue_session = wx.getStorageSync("yuyue_session");
    wx.request({//获取信息
      url: app.globalData.default_url,
      data: {
        action: 'api.v1.InternalUser.getInfo',
        yuyue_session: yuyue_session,
      },
      success: function (res) {
        console.log(res);
        if (res.data.data.status == 1) {
          
          that.setData({
            sign:true,
            bind_phoneNumber: res.data.data.phoneNumber,
            nickName: app.globalData.userInfo.nickName,
            avatar: app.globalData.userInfo.avatarUrl,
          })
        }
      }, fail: function (res) {
        console.log(res);
      }
    })
  },

  //存电话号
  bindphonenumber: function (e) {
    var bindover = true;
    var phonenumber = e.detail.value;
    var regLowerCase = new RegExp('[0-9]{11}', 'g');

    if (regLowerCase.exec(phonenumber)) {//格式验证
      this.setData({
        bindpover: bindover,
        phonenumber_regtrue: true,
        phonenumber: phonenumber,

      })
    } else {
      this.setData({
        phonenumber_regtrue: false,
        bindpover: bindover,
        phonenumber: phonenumber
      })

    }
    /*
                console.log(" bindpover: ", this.data.bindpover);
              console.log(" bindvover: ", this.data.bindvover);
              console.log(" phonenumber_regtrue: ", this.data.phonenumber_regtrue);
              console.log(" code_regtrue: ", this.data.code_regtrue);
        console.log(" bindpover: true");
    */
    // console.log(  phonenumber);

  },
  inputphonenumber: function (e) {
    console.log(" bindpover: false");
    this.setData({
      bindpover: false,

    })
  },

  //存verifycode
  bindverifycode: function (e) {
    var bindover = true;
    var verify_code = e.detail.value;
    var regLowerCase = new RegExp('[0-9]{4}', 'g');

    if (regLowerCase.exec(verify_code)) {//格式验证
      this.setData({
        bindvover: bindover,
        code_regtrue: true,
        verify_code: verify_code,

      })
    } else {
      this.setData({
        code_regtrue: false,
        bindvover: bindover,
        verify_code: verify_code,
      })

    }
    /*
    console.log(" bindpover: ", this.data.bindpover);
    console.log(" bindvover: ", this.data.bindvover);
    console.log(" phonenumber_regtrue: ", this.data.phonenumber_regtrue);
    console.log(" code_regtrue: ", this.data.code_regtrue);
    console.log(" bindvover: true");
*/
    // console.log(  phonenumber);

  },
  /**
   * 输入 
   */
  inputverifycode: function (e) {
    console.log(" bindvover: false");
    this.setData({
      bindvover: false,

    })
  },
  /**
   * 短信验证时间间隔
   */
  verify_time: function () {
    var that = this;
    var i = 60;
    // console.log("i=", i);
    var inter = setInterval(function () {

      if (i > 0) {
        that.setData({
          time: "(" + i + ")",
        })
        i--;
      } else if (i == 0) {
        that.setData({
          time: " ",
        })
        i--;
      } else {
        clearInterval(inter);
      }

    }, 1000);


  },
  sign_nav: function () {
    this.setData({
      sign: false,
    })
  },
  //服务器

  //验证电话号是否符合，获取验证码
  verifycode_request: function () {
    console.log(this.data.time)
    if (this.data.time != " ") {
      return;
    }
    var regLowerCase = new RegExp('[0-9]{11}', 'g');
    var phonenumber = this.data.phonenumber;
    if (regLowerCase.exec(phonenumber)) {
      this.verify_time();
      var yuyue_session = wx.getStorageSync('yuyue_session');
      var token = wx.getStorageSync('token');
      wx.request({
        url: app.globalData.default_url,
        data: {
          action: 'api.v1.InternalUser.send',
          phoneNumber: phonenumber,
          nationCode: '86',
          yuyue_session: yuyue_session,
          token: token,
        },
        success: function (res) {
          if (res.data.data)
            if (res.data.data.result.result == "0")
              console.log(res);
            else {
              console.log(res);
              wx.showModal({
                title: '操作失败',
                showCancel: false,
                content: res.data.data.result.errmsg,
              })
            } else {
            console.log(res);
            wx.showToast({
              title: '请求失败',
              icon: 'error',

              duration: 1000,
              mask: true,
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          }

        }, fail: function (res) {
          console.log(res);
          console.log(1234);
        },
      });
    } else {
      wx.showModal({
        title: '输入错误',
        content: '请输入正确手机号',
        showCancel: false,
        confirmText: '确定',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  verify_request: function (e) {//登录注册验证请求
    console.log("verify_request:e :", e);
    var yuyue_session = wx.getStorageSync('yuyue_session');
    var phonenumber = e.detail.value.phonenumber;

    var verify_code = e.detail.value.verificationcode
    wx.request({
      url: app.globalData.default_url,
      data: {
        action: 'api.v1.InternalUser.verify',
        phoneNumber: phonenumber,
        nationCode: '86',
        yuyue_session: yuyue_session,
        verify_code: verify_code,
      },
      success: function (res) {

        console.log("success:", res);
        if (res.data.op == "verify") {

          var data = res.data.data;
          if (data.status == 0) {//出现问题
            wx.showModal({
              title: '不好啦',
              content: data.reason,
              showCancel: false,

            })
            return;
          } else {
            if (data.status == 3) {
              wx.setStorageSync("yuyue_session", data.yuyue_session);
            }
            wx.setStorageSync("login", true);
            wx.switchTab({
              url: '../mine/index'
            })
            return;
          }
        } else {//服务器连接故障
          wx.showModal({
            title: '服务器连接故障',
            content: '请联系管理员',
            showCancel: false,
            success: function (res) {

            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showModal({
          title: '网络连接连接故障',
          content: '',
          showCancel: false,
          success: function (res) {

          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })



  },

  dologin_request: function () {
var that = this;
    var yuyue_session = wx.getStorageSync("yuyue_session");
  
        wx.request({//获取信息
          url: app.globalData.default_url,
          data: {
            action:  'api.v1.InternalUser.login',
            yuyue_session: yuyue_session,
          },
          success: function (res) {
            console.log("dologin_request" ,res);
            if (res.data.data.status == 1) {
              wx.setStorageSync("yuyue_session", res.data.data.yuyue_session);
              wx.setStorageSync("uid", res.data.data.uid);
              wx.setStorageSync("login", true);
              that.setData({
                bind_phoneNumber: res.data.data.phoneNumber,
                nickName: app.globalData.userInfo.nickName,
                avatar: app.globalData.userInfo.avatarUrl,
              })
              wx.navigateBack({
                
              })
            }else{
              wx.showModal({
                title: '系统错误',
                content: res.data.data.reason ,
                showCancel:false,
                success:function(res){
                  if(res.confirm){
                    wx.navigateBack({
                      
                    })
                  }
                }
              })
            }
          }, fail: function (res) {
            console.log(res);
          }
        })
  }
})
